﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample.LINQ
{
  class LINQ
  {
    static List<int> m_ListInt = new List<int>() { 1, 5, 8, 12, 13, 19 };

    //LINQ
    static void Sample1()
    {
      //Selection des nombres > 10 et ordonner la liste

      //FOREACH
      var result = new List<int>();
      foreach (int nbr in m_ListInt) //from n in m_ListInt
      {
        if (nbr > 10)//where n > 10
        {
          result.Add(nbr); //select n
        }
      }
      result.Sort();//orderby n

      //LINQ
      var resultLINQ =
       from n in m_ListInt
       where n > 10
       orderby n ascending
       select n; 
    }

    //Expression Lambda
    static void Sample2()
    {
      //Premier nombre > 10

      //FOREACH
      var result = int.MinValue;
      foreach (int nbr in m_ListInt)
      {
        if (nbr > 10)
        {
          result = nbr;
          break;
        }
      }

      //LINQ + Expression Lambda
      var resultLINQ = m_ListInt.First(l => l > 10);
    }

    //Expression ?:
    static void Sample3()
    {
      int value = 5;
      string result;
      //if
      if (value == 5)
      {
        result = "OK";
      }
      else
      {
        result = "NOK";
      }

      //Expression
      result = value == 5 ? "OK" : "NOK";
    }

    //Expression ??
    static void Sample4()
    {
      object result;
      object myObject = null;
      object myObjectIfNull = null;
      //if
      if (myObject != null)
      {
        result = myObject;
      }
      else
      {
        result = myObjectIfNull;
      }

      //Expression
      result = myObject ?? myObjectIfNull;
    }
  }
}
